package com.coldchain.message.Listener;

import com.coldchain.message.core.constant.QueryType;
import com.coldchain.message.core.entity.order.*;
import com.coldchain.message.core.im.IMMessage;
import com.coldchain.message.im.base.BaseIMAccount;
import com.coldchain.message.im.enums.IMType;
import com.coldchain.message.im.service.IMService;
import com.coldchain.message.im.service.impl.IMAccountService;
import com.coldchain.message.push.PushAccountService;
import com.coldchain.message.push.PushTools;
import com.coldchain.message.webscoker.WebsocketStarter;
import com.fangliai.core.entitiy.SystemModelEnum;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


/**
 * @ClassName OrderListener
 * @Description TODO
 * @Author fangliai
 * @Date 2018/9/20 17:25
 * @Vesion 1
**/
 @Component
 //@TODO 现有的第三方IM服务非常不省心啊! 业务场景适配很难！是否有需要自行开发一套IM服务呢!
public class OrderListener {
     private static  final Logger log= LoggerFactory.getLogger(OrderListener.class);
     @Autowired
     private IMAccountService imAccountService;

     @Autowired
     private PushAccountService pushAccountService;
    /**
     * 新订单事件
     * @param newOrderMsg
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueryType.NEW_ORDER_QUERY)
    public void onNeweOrderMsg(@Payload com.coldchain.message.core.entity.Message<NewsMsg> newOrderMsg, Message message, Channel channel){
              try {
                  WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.USER.name(),newOrderMsg);
                  WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.ADMIN.name(),newOrderMsg);
            } catch (Exception e) {
                  log.error("onNeweOrderMsg send error:"+e.getMessage());
            }

    }

    /**
     * 帮助订单事件
     * @param helpOrderMsg
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueryType.HELP_ORDER_QUERY)
    public void onHelpOrderMsg(@Payload com.coldchain.message.core.entity.Message<HelpMsg> helpOrderMsg, Message message, Channel channel){

       try {
           WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.USER.name(),helpOrderMsg);
           WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.ADMIN.name(),helpOrderMsg);
         }catch (Exception e){
           log.error("onHelpOrderMsg send error:"+e.getMessage());
         }
    }

    /**
     * 订单完成事件
     * @param completeMsg
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueryType.COMPLETE_QUERY)
    public void onOrderCompleteMsg(@Payload com.coldchain.message.core.entity.Message<CompleteMsg> completeMsg, Message message, Channel channel){
        try {
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.USER.name(),completeMsg);
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.ADMIN.name(),completeMsg);

        }catch (Exception e){
            log.error("onOrderCompleteMsg send error:"+e.getMessage());
        }
    }

    /**
     * 配送点送达事件
     * @param deliveryMsg
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueryType.DELIVERY_QUERY)
    public void oneDeliveryMsg(@Payload com.coldchain.message.core.entity.Message<DeliveryMsg> deliveryMsg, Message message, Channel channel){

        try {
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.USER.name(),deliveryMsg);
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.ADMIN.name(),deliveryMsg);

        }catch (Exception e){
            log.error("oneDeliveryMsg send error:"+e.getMessage());
        }
    }

    /**
     * 调派事件
     * @param dispatchMsgMessage
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueryType.DISPATCH_QUERY)
    public void oneDispatchMsg(@Payload com.coldchain.message.core.entity.Message<DispatchMsg> dispatchMsgMessage, Message message, Channel channel){

        try {
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.USER.name(),dispatchMsgMessage);
            WebsocketStarter.getInstance().sendGroupMsg(SystemModelEnum.ADMIN.name(),dispatchMsgMessage);

        }catch (Exception e){
            log.error("oneDispatchMsg send error:"+e.getMessage());
        }
    }

    private PushTools getPushTools(){
        PushAccountService.JiguangPushAccount account=pushAccountService.getPushAccount();
        return new PushTools(account);
    }

    private void sendIMMessage(IMMessage message)throws Exception{
        BaseIMAccount account=imAccountService.getAccount();
        IMService imService=IMType.valueOf(account.getImType()).getIMService();
        imService.setAccount(account);
        imService.sendMessage(message);

    }
}
